package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.basic.sequence.service.facade.SequenceService;
import com.fdb.efp.console.common.ConsoleConstant;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.nls.api.service.RiskmUtilService;
import com.fdb.efp.nls.service.facade.MonReliefAppService;
import com.fdb.efp.nls.service.vo.MonReliefAppVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 息费减免申请
 */
@RestController
@RequestMapping("/api")
public class MonReliefAppController extends AbstractController {

    @Autowired
    @Qualifier("monReliefAppService")
    private MonReliefAppService monReliefAppService;

    @Autowired
    @Qualifier("sRoleDataRuleService")
    private SRoleDataRuleService sRoleDataRuleService;

    @Autowired
    private RiskmUtilService riskmUtilService;

    @Autowired
    @Qualifier("dbSeqService")
    private SequenceService sequenceService;

    /**
     * 列表数据查询
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/mon/relief/apps", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<MonReliefAppVO>> queryMonReliefAppAll(@RequestBody MonReliefAppVO vo) {
        List<MonReliefAppVO> outputVo = null;
        // 根据用户角色获取用户权限方案
        Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null : dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
        if(StringUtil.isStrNotEmpty(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())){
            logger.info("======>开始导出息费减免申请信息数据<======");
            switch (dataRuleType) {
                case Owner:
                    outputVo = monReliefAppService.queryAllOwnerExp(vo);
                    break;
                case CurrOrg:
                    outputVo = monReliefAppService.queryAllCurrOrgExp(vo);
                    break;
                case CurrDownOrg:
                    outputVo = monReliefAppService.queryAllCurrDownOrgExp(vo);
                    break;
                case CurrOwnerPrd:
                    outputVo = monReliefAppService.queryAllCurrOwnerPrdExp(vo);
                    break;
                default:
                    outputVo = monReliefAppService.queryAllOwnerExp(vo);
                    break;
            }
        }else{
            logger.info("======>开始查询息费减免申请信息<======");
            switch (dataRuleType) {
                case Owner:
                    outputVo = monReliefAppService.queryAllOwner(vo);
                    break;
                case CurrOrg:
                    outputVo = monReliefAppService.queryAllCurrOrg(vo);
                    break;
                case CurrDownOrg:
                    outputVo = monReliefAppService.queryAllCurrDownOrg(vo);
                    break;
                case CurrOwnerPrd:
                    outputVo = monReliefAppService.queryAllCurrOwnerPrd(vo);
                    break;
                default:
                    outputVo = monReliefAppService.queryAllOwner(vo);
                    break;
            }
        }
        riskmUtilService.displayUserName(outputVo,"aprvUserCode","aprvUserName");
        return getResponseData(outputVo);

    }



    /**
     * 根据主键查询详情
     *
     * @return
     */
    @RequestMapping(value = "/mon/relief/app", method = RequestMethod.GET)
    public @ResponseBody
    ResponseData<MonReliefAppVO> queryByPk() {
        MonReliefAppVO inVo = new MonReliefAppVO();
        MonReliefAppVO outputVo = monReliefAppService.queryByPk(inVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键删除信息
     *
     * @param inMonReliefAppVo
     * @return
     */
    @RequestMapping(value = "/mon/relief/app", method = RequestMethod.DELETE)
    public @ResponseBody
    ResponseData<Integer> deleteByPk(@RequestBody MonReliefAppVO inMonReliefAppVo) {
        int outputVo = monReliefAppService.deleteByPk(inMonReliefAppVo);
        return getResponseData(outputVo);

    }

    /**
     * 根据主键更新信息
     *
     * @param inMonReliefAppVo
     * @return
     */
    @RequestMapping(value = "/mon/relief/app", method = RequestMethod.PUT)
    public @ResponseBody
    ResponseData<Integer> updateByPk(@RequestBody MonReliefAppVO inMonReliefAppVo) {
        int outputVo = monReliefAppService.updateByPk(inMonReliefAppVo);
        return getResponseData(outputVo);

    }

    @RequestMapping(value = "/mon/relief/app/check/loan", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<Integer> checkLoanIsExistApp(@RequestBody MonReliefAppVO vo) {
        List<MonReliefAppVO> monReliefAppVOList = monReliefAppService.queryAllByConditions(vo);
        if(Objects.nonNull(monReliefAppVOList) && monReliefAppVOList.size() > 0){
            long count = monReliefAppVOList.stream()
                    .map(MonReliefAppVO::getApproveStatus)
                    .filter(s -> {
                        return ConsoleConstant.APRV_STATUS_01.equals(s) || ConsoleConstant.APRV_STATUS_02.equals(s) || ConsoleConstant.APRV_STATUS_05.equals(s);
                    })
                    .count();
            if(count > 0){
                return getResponseData(-1);
            }
        }
        return getResponseData(0);
    }

    @RequestMapping(value = "/mon/relief/detail", method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<MonReliefAppVO>> queryMonReliefDetail(@RequestBody MonReliefAppVO vo) {
        List<MonReliefAppVO> outputVo = null;
        if(StringUtil.isStrNotEmpty(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())){
            logger.info("======>开始导出息费减免明细信息数据<======");
            outputVo = monReliefAppService.queryAllDetailExp(vo);
        }else{
            logger.info("======>开始查询息费减免明细信息<======");
            outputVo = monReliefAppService.queryAllDetail(vo);
        }
        return getResponseData(outputVo);
    }

    @RequestMapping(value = "/get/relief/app/no", method = RequestMethod.GET)
    public @ResponseBody
    ResponseData<String> getReliefAppNo() {
        try {
            return getResponseData(sequenceService.getSequence("RELIEF_APP_NO", null, null));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.create(null, ApplicationConstance.FAILURE, "生成减免申请流水号失败，原因:"+e.getMessage(), false);
        }
    }

}
